package cc.wangzijie.ddns.aliyun.client;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * @author wangzijie
 */
@Slf4j
@Component
public class DdnsExecutor implements ApplicationRunner {

    @SuppressWarnings("AlibabaThreadPoolCreation")
    private final ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);

    @Value("${ddns.domainName}")
    private String domainName;

    @Value("${ddns.initialDelay}")
    private Long initialDelay;

    @Value("${ddns.period}")
    private Long period;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("========[ Init Param ]========");
        log.info("ddns.domainName = {} (default: 'wangzijie.store')", domainName);
        log.info("ddns.initialDelay = {} (second)", initialDelay);
        log.info("ddns.period = {} (second)\n\n", period);
        DdnsTask task = new DdnsTask(domainName);
        scheduledExecutorService.scheduleWithFixedDelay(task, initialDelay, period, TimeUnit.SECONDS);
    }
}
